<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <!-- 编辑时才显示-->
                  <a-form-model-item label="资产处置申请编码">
                    <BaseInput
                      v-model="formData.code"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="资产类别">
                    <a-select
                      v-model="formData.assetsTypeId"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in categoryArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="资产所属单位"
                    prop="ownerOrgStr"
                  >
                    <a-input
                      v-model="formData.ownerOrgStr"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="编号"
                    prop="number"
                  >
                    <BaseInput
                      v-model="formData.number"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="金额合计（元）">
                    <BaseInput
                      v-model="formData.sumAmount"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="建议处置方式">
                    <a-select
                      v-model="formData.disposalMethodId"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in wayArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-if="state !== 3"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="处置明细">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <detail
              :id="formData ? formData.id : null"
              ref="detail"
              :state="state"
              @confirm="getDetails"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :upload-list.sync="addFile"
              :delete-url="fileDel"
              :is-show="state !== 3"
              :list-url="fileList"
              :business-id="formData.id"
              business-code="MM01"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div
        v-if="!isAudit"
        class="pub-header-btn page-btn-right-top"
      >
        <template v-if="state !== 3">
          <a-button
            type="primary"
            :loading="loading"
            @click="submit"
          >
            提交
          </a-button>
          <a-button
            type="primary"
            :loading="loading"
            @click="temporaryStorage"
          >
            保存
          </a-button>
        </template>
        <a-button
          v-checkVisibleInDetail
          @click="$close"
        >
          关闭
        </a-button>
      </div>
      <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover> 
    </base-breadcrumb>
  </div>
</template>

<script>
import detail from '../components/detail/detail'
import { selectOption } from '@/api/material/goods/goods'
import {
    disposalAdd,
    disposalEdit,
    audit,
    addFile,
    fileList,
    fileDel,
    disposalapply,
    findListByMainId
} from '@/api/material/assets/disposal'

export default {
	name:'a'+Date.now(),
    components: {
        detail
    },
    props: {
        isAudit: { // 是否作为审批详情
            type: Boolean,
            default: false
        }
    },
    watch: {
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.state = 3
                }
            }
        },
        stepVal(newVal, oldVal) {
            this.$set(this.activeKey, newVal, true)
        }
    },
    data() {
        return {
            activeKey: [true, false, false, false, false, false, false],
            loading: false,
            state: Number(this.$route.query.state),
            isSubmit: false,
            formData: {
                createTime: this.getNowTime(),
                creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
                ownerOrgStr: '',
                ownerOrgId: '',
                assetsTypeId: '',
                disposalMethodId: ''
            },
            isNext: false,
            stepVal: 0,
            addFile: addFile(),
            fileDel: fileDel(),
            fileList: fileList(),
            categoryArr: [],
            rules: {
                number: { required: true, message: '编号不能为空', trigger: 'blur' },
                ownerOrgStr: { required: true, message: '资产所属单位不能为空', trigger: 'change' }
            },
            wayArr: [],
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
    created: async function() {
        this.loading = true
        if(this.state === 1) {
            this.formData.ownerOrgStr = this.$currentPost().name
            this.formData.ownerOrgId = this.$currentPost().id
        }

        await this.getCategory()
        await this.getWay()
        if (this.$route.query.id && this.state !== 1) {
            this.formData.id = this.$route.query.id
            await this.getDetails()
            this.isSubmit = true
        }
        this.loading = false
        //获取通过获取流程配置值
        this.processConfig = await this.$getConfigFn('mm_assets_apply')

    },
    methods: {
        async getDetails() {
            this.loading = true
            let res = await findListByMainId({ id: this.formData.id })
            if (res.code === 200) {
                this.formData = this.$assign(this.formData, res.data)
            }
            this.loading = false
        },
        async getCategory() {
            let res = await selectOption({ type: 25 })
            if (res.code === 200) {
                this.categoryArr = res.data
                this.formData.assetsTypeId = this.categoryArr[0].id
            }
        },
        async getWay() {
            let res = await selectOption({ type: 26 })
            if (res.code === 200) {
                this.wayArr = res.data
                this.formData.disposalMethodId = this.wayArr[0].id
            }
        },

        stepChange(val) {
            if (!this.formData.id && !this.isAudit) {
                if (val) {
                    this.$message.warning('切换步骤前请先填写基本信息')
                    this.stepVal = 0
                }
            } else {
                if (!this.isNext) {
                    this.$set(this.activeKey, val, !this.activeKey[val])
                }
            }
            this.isNext = false
        },
        next(val) {
            this.isNext = true
            this.stepVal++
        },
        previous() {
            this.isNext = true
            this.stepVal--
            if (this.stepVal < 0) {
                this.stepVal = 0
            }
        },
        submit() {
            // this.save('approve');
            this.setProcessConfig = null;
            if (this.isSubmit) {
                this.submitApprovalConfirm(this.isSubmit?this.processConfig:6, async(flag)=>{
                  if(!flag) {
                    this.loading = false
                    return
                  }
                    this.loading = true
                    disposalapply({
                        baseId: this.formData.id,
                        auditStatus: this.formData.auditStatus
                    }).then(res => {
                        this.$message.success('提交成功')
                        this.loading = false
                        //提交时，返回id
                        this.businessId = this.formData.id;
                        this.setProcessConfig = this.processConfig;
                        // this.$router.push({ path: '/material/assets/disposal' })
                    })
                })
            } else {
                this.$message.warning('提交之前请先暂存')
            }
        },
        temporaryStorage(isNext) {
            if (this.state === 3) {
                this.next()
                return
            }
            this.$refs.ruleForm.validate((valid, object) => {
                if (valid) {
                    this.loading = true
                    if (this.state === 2 || this.formData.id) {
                        disposalEdit(this.formData).then(res => {
                            if (res.code === 200) {
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    } else {
                        disposalAdd(this.formData).then(res => {
                            if (res.code === 200) {
                                this.formData.id = res.data.id
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                this.getDetails()
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    }
                } else {
                    return false
                }
            })
        }
    }
}
</script>
